using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Zombie : Entity
{
    public ZombieType ZombieType { get; private set; }

    /// <summary>
    /// 被嘲讽的目标（如果有TauntBuff，优先攻击此目标）
    /// </summary>
    public Entity TauntTarget { get; set; }

    private SpriteRenderer[] m_SpriteRenderers;
    private Color[] m_OriginalColors;
    private Tween m_ColorTween;

    public override void Initialize()
    {
        base.Initialize();
        
        // 获取所有 SpriteRenderer 并保存原始颜色
        m_SpriteRenderers = GetComponentsInChildren<SpriteRenderer>();
        if (m_SpriteRenderers != null && m_SpriteRenderers.Length > 0)
        {
            m_OriginalColors = new Color[m_SpriteRenderers.Length];
            for (int i = 0; i < m_SpriteRenderers.Length; i++)
            {
                m_OriginalColors[i] = m_SpriteRenderers[i].color;
            }
        }
    }

    public override void SetColor(Color color, float duration = 0.1f)
    {
        Logger.Log("Zombie", $"{name} SetColor，改变颜色");
        if (m_SpriteRenderers == null || m_SpriteRenderers.Length == 0)
        {
            // 如果还没有初始化，尝试获取
            m_SpriteRenderers = GetComponentsInChildren<SpriteRenderer>();
            if (m_SpriteRenderers != null && m_SpriteRenderers.Length > 0)
            {
                m_OriginalColors = new Color[m_SpriteRenderers.Length];
                for (int i = 0; i < m_SpriteRenderers.Length; i++)
                {
                    m_OriginalColors[i] = m_SpriteRenderers[i].color;
                }
            }
            else
            {
                Logger.LogWarning("Zombie", $"{name} 没有找到 SpriteRenderer，无法改变颜色");
                return;
            }
        }

        // 停止之前的颜色动画
        if (m_ColorTween != null && m_ColorTween.IsActive())
        {
            m_ColorTween.Kill();
        }

        // 立即设置目标颜色
        for (int i = 0; i < m_SpriteRenderers.Length; i++)
        {
            if (m_SpriteRenderers[i] != null)
            {
                m_SpriteRenderers[i].color = color;
            }
        }

        // 如果 duration > 0，则在 duration 后恢复原色
        if (duration > 0)
        {
            m_ColorTween = DOVirtual.DelayedCall(duration, () =>
            {
                RestoreOriginalColor();
            });
        }
    }

    /// <summary>
    /// 恢复原始颜色
    /// </summary>
    private void RestoreOriginalColor()
    {
        if (m_SpriteRenderers != null && m_OriginalColors != null)
        {
            for (int i = 0; i < m_SpriteRenderers.Length && i < m_OriginalColors.Length; i++)
            {
                if (m_SpriteRenderers[i] != null)
                {
                    m_SpriteRenderers[i].color = m_OriginalColors[i];
                }
            }
        }
    }

    protected override void OnDestroy()
    {
        // 清理颜色动画
        if (m_ColorTween != null && m_ColorTween.IsActive())
        {
            m_ColorTween.Kill();
        }
        base.OnDestroy();
    }
 }
